package com.xxx.car.web;

import com.alibaba.fastjson.JSON;
import com.xxx.car.service.TradeService;
import com.xxx.common.pay.core.vo.TradeInfo;
import com.xxx.common.pay.service.PayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by xuwenjie on 2017/6/16.
 */
@Controller
@RequestMapping("/pay")
public class PayController {

    private static final Logger LOG = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private PayService payService;

    @Autowired
    private TradeService tradeService;

    @RequestMapping("/callback/{payChannel}")
    public void callBack(@PathVariable("payChannel") String payChannel, HttpServletRequest request, HttpServletResponse response) {
        TradeInfo info = payService.notifyValidate(payChannel, request);
        LOG.info(JSON.toJSONString(info));
        tradeService.finishPay(info.getOrderId(), info.getTransactionId(), info.getAmt());
    }
}
